package com.awspaas.user.apps.shhtaerospaceindustrial.apartmanage;

import java.util.List;
import java.util.Map;

import com.actionsoft.bpms.bpmn.engine.core.delegate.ProcessExecutionContext;
import com.actionsoft.bpms.bpmn.engine.listener.ExecuteListener;
import com.actionsoft.bpms.commons.database.ColumnMapRowMapper;
import com.actionsoft.bpms.util.DBSql;
import com.actionsoft.sdk.local.SDK;
import com.awspaas.user.apps.shhtaerospaceindustrial.healthmeaasge.weComSendAnnouncementUtil;
import com.awspaas.user.apps.shhtaerospaceindustrial.util.CoreUtil;

public class ApartNoticeSendMessage extends ExecuteListener{

	public String getDescription() {
		return "人才公寓通知公告审批结束，给相关人员发企业微信消息。";
	}
	
	@Override
	public void execute(ProcessExecutionContext param) throws Exception {
		boolean flag_ok = SDK.getTaskAPI().isChoiceActionMenu(param.getTaskInstance(), "同意");
		if(flag_ok) {
			//获取流程实例ID
			String processInstId = param.getProcessInstance().getId();
			//根据流程实例ID获取任务表内数据
			List<Map<String , Object>> headlist = DBSql.query("SELECT * FROM BO_EU_APARTMENTNOTICE  WHERE BINDID = ? ", 
					new ColumnMapRowMapper(), new Object[] {processInstId});
			Map<String , Object>  jcbd = headlist.get(0);
			String BOID = CoreUtil.objToStr(jcbd.get("ID"));
			String TITLE=CoreUtil.objToStr(jcbd.get("TITLE"));
			String CONTENT=CoreUtil.objToStr(jcbd.get("CONTENT"));
			String XGFILE=CoreUtil.objToStr(jcbd.get("XGFILE"));
			weComSendAnnouncementUtil.uploadFile(BOID,"XGFILE");
			String content = "【人才公寓管理——"+TITLE+"】 ："+CONTENT;
			if(XGFILE!="") {
				String url="https://www.sht808.com/portal/upload/"+XGFILE;
				content+="\n <a href=\\\""+url+"\\\">通知附件</a>";
			}
			String querySql ="select t.UNIT, t.APARTLEADERZH from BO_EU_SAST_UNIT_BWBM t where t.isglrcgy ='是'  order by t.orderbyno asc";
			 List<Map<String, Object>> urlList = DBSql.query(querySql, new ColumnMapRowMapper(), new Object[] {});
			 for (int i = 0; i < urlList.size(); i++) {
				 StringBuffer ygstring = new StringBuffer();//需发送企业微信用户
				 Map<String, Object> urlMap = urlList.get(i);
				 String UNIT = CoreUtil.objToStr(urlMap.get("UNIT"));// 单位简称
				 String APARTLEADERZH = CoreUtil.objToStr(urlMap.get("APARTLEADERZH"));// 人才公寓管理员企业微信账号
				 if(!"".equals(UNIT) && !"".equals(APARTLEADERZH)) {
					 String Sqlygcheck ="select t.EXT5 from VIEW_RCGY_ROOM_TOTAL  t   where t.belongunit ='"+UNIT+"' and t.YUANGONGZH is not null";
					 List<Map<String, Object>> yglList = DBSql.query(Sqlygcheck, new ColumnMapRowMapper(), new Object[] {});
					 if(yglList == null || yglList.isEmpty()) {
						 ygstring.append(APARTLEADERZH.replaceAll(",", "|")) ;
					 }else {
						 for (int j = 0; j < yglList.size(); j++) {
							 Map<String, Object> gyMap = yglList.get(j);
							 String EXT5 = CoreUtil.objToStr(gyMap.get("EXT5"));// 企业微信账号
							 if(!"".equals(EXT5)){
								 ygstring.append(EXT5);
								 ygstring.append("|"); 
							 }
						}
						 ygstring.append(APARTLEADERZH.replaceAll(",", "|")) ;
					 }
					 String sql = "select t.*  from BO_EU_SHSY_NOTICE_UNITLIST t  where t.unit ='"+UNIT+"'";
				     List<Map<String, Object>> jbData = DBSql.query(sql, new ColumnMapRowMapper(), new Object[] {});
				     if (jbData.size() > 0 ) {
					        String agentid = CoreUtil.objToStr(jbData.get(0).get("AGENTID"));//应用id
							String corpid = CoreUtil.objToStr(jbData.get(0).get("CORPID"));// 企业微信单位id
							String agentsecretid = CoreUtil.objToStr(jbData.get(0).get("AGENTSECRETID"));//应用secretid
							String acessToken = weComSendAnnouncementUtil.getAccessToken(corpid,agentsecretid);
							weComSendAnnouncementUtil.sendWeChatMessage(ygstring.toString(),"","text","",content,0,agentid,acessToken);
				        }
				 }
			}
		}
			
	}

}
